<!DOCTYPE html>
<html>

<head>
    <title>Preview Display</title>
    <meta charset="UTF-8">
    <!--    <link rel="shortcut icon" href="custom/images/favicon.ico?v=2"/>-->
    <style>
        html,
        body {
            padding: 0px;
            margin: 0px;
        }

        body {
            background: #000 url(../storage/assets/兴达八厂/车间背景.jpg) no-repeat left top;
            background-size: 100%;
        }
    </style>
    <script src="../libs/jquery-3.4.1.min.js"></script>
    <script src='../libs/message.js'></script>
    <!--    <script src='../custom/configs/htconfig.js'></script>-->
    <script src='../custom/libs/echarts.js'></script>
    <script src='../libs/core/ht.js'></script>
    <script src='../libs/core/ht-ui.js'></script>
    <script src='../libs/plugin/ht-edgetype.js'></script>
    <script src='../libs/plugin/ht-obj.js'></script>
    <script src='../libs/plugin/ht-form.js'></script>
    <script src='../libs/plugin/ht-vector.js'></script>
    <script src='../conf/confURL.js'></script>
    <script src='../conf/dateTime.js'></script>
    <script src='../conf/workshopAjax.js'></script>
    <!-- <script>
         //color.js报警配置文件
         document.write('<scr' + 'ipt src="' + layout_editorURL + '/config/color.js"></scr' + 'ipt>');
     </script>-->
    <script>
        dataModel = new ht.DataModel();
        graphView = new ht.graph.GraphView(dataModel);
        // 厂区编码
        let deptCode = GetQueryString('deptCode');
        let strDeptCode = deptCode.substring(deptCode.lastIndexOf("_") + 1, deptCode.length);
        let tag;

        // 监控类型
        let sceneType = GetQueryString('sceneType');
        // 车间编码
        // 0527 : 车间及报表接口传参workshopCode时，传的是截取后的XD111203,后续可能会改为完整车间编码
        let workshopCode = GetQueryString('workshopCode');
        workshopCode = workshopCode.substring(workshopCode.lastIndexOf("_") + 1, workshopCode.length);
        let backButton = '';
        let normal = '';             // 正常
        let fixedLength = '';        // 到定长
        let wireBreak = '';          // 断丝
        let changeover = '';         // 换模
        let wireReplacement = '';    // 换丝
        let breakdown = '';          // 有、无故障停机
        let offLine = '';           // 脱机
        let plannedStop = '';       // 计划停台

        let iframe = '';
        let iframeId = '';
        let iframe2 = '';
        let tableBtn = '';
        let dateTime;

        let workshop1 = '';
        let workshop2 = '';
        let workshop3 = '';

        let workshop_nine1 = '';
        let workshop_nine21 = '';
        let workshop_nine22 = '';
        let workshop_nine3 = '';
        let workshop_nine4 = '';

        // 八厂
        let workshopCode1 = 'XD111203';   // 一车间编码、tag
        let workshopCode2 = 'XD111204';   // 二车间编码、tag
        let workshopCode3 = 'XD111205';   // 三车间编码、tag
        dataModel.a('workshopCode1', workshopCode1);
        dataModel.a('workshopCode2', workshopCode2);
        dataModel.a('workshopCode3', workshopCode3);

        // 九厂
        let workshopCode_nine1 = 'XD111303';   // 一车间编码、tag
        let workshopCode_nine21 = 'XD11130401';   // 二车间一期编码、tag
        let workshopCode_nine22 = 'XD11130402';   // 二车间二期编码、tag
        let workshopCode_nine3 = 'XD111305';   // 三车间编码、tag
        let workshopCode_nine4 = 'XD111306';   // 四车间编码、tag
        dataModel.a('workshopCode_nine1', workshopCode_nine1);
        dataModel.a('workshopCode_nine21', workshopCode_nine21);
        dataModel.a('workshopCode_nine22', workshopCode_nine22);
        dataModel.a('workshopCode_nine3', workshopCode_nine3);
        dataModel.a('workshopCode_nine4', workshopCode_nine4);

        if ("XD1112" === strDeptCode) {
            tag = "workshop";
        } else if ("XD1113" === strDeptCode) {
            tag = "workshop_nine";
        }

        function init() {
            graphView.addToDOM();
            ht.Default.xhrLoad('displays/智慧兴达/兴达八厂/生产监控/'+ tag +'.json', function (text) {
                var json = ht.Default.parse(text);
                if (json.title) document.title = json.title;
                dataModel.deserialize(json);
                // 颜色可配置
                // dataModel.a('color_config', color_config);
                dataModel.a('workshopCode', workshopCode);
                //设置编辑器地址
                dataModel.a('layout_editorURL', layout_editorURL);
                graphView.fitContent(true);
                // 获取时间
                dateTime = dataModel.getDataByTag('dateTime');
                setInterval(function () {
                    let time = updateTime();
                    dateTime.a('time', time);
                }, 1000);
                // 获取标签
                initParams();
                // 厂区进入车间
                let shopData = dataModel.getDataByTag(workshopCode);

                // shopData.s('onClick')(event, shopData, graphView);
                // 报警灯
                getWarningLights(workshopCode);
                // 车间返回按钮
                getWorkShopBackButton(deptCode, sceneType);
                // 车间按钮显示
                getDeptChange(deptCode, sceneType);
                setInterval("getWarningLights(workshopCode)", 1000 * 20);
            });
        }

        let messageParam = {};
        graphView.addInteractorListener(e => {
            const kind = e.kind;
            if (kind === 'clickData') {
                if (e.data._displayName === "cameraBtn") {
                    messageParam.flag = "camera";
                    messageParam.message = "true";
                    if (Boolean(e.data.a('isSelected'))) messageParam.message = "false";
                    iframeMessage(messageParam);
                } else if (e.data._displayName === "tab") {
                    // workshopCode = dataModel.a('workshopCode');
                    workshopCode = e.data.getTag();
                    getWarningLights(workshopCode);
                }
            }
        });

        // 给子页面index.html传值
        function iframeMessage(message) {
            let iframe = $('#iframe')[0];
            iframe.contentWindow.postMessage(message, '*');
        }

        function initParams() {
            workshopCode = dataModel.a('workshopCode');
            // 返回按钮
            backButton = dataModel.getDataByTag('backButton');
            // 报警灯
            normal = dataModel.getDataByTag('normal');
            fixedLength = dataModel.getDataByTag('fixedLength');
            wireBreak = dataModel.getDataByTag('wireBreak');
            changeover = dataModel.getDataByTag('changeover');
            wireReplacement = dataModel.getDataByTag('wireReplacement');
            breakdown = dataModel.getDataByTag('breakdown');
            offLine = dataModel.getDataByTag('offLine');
            plannedStop = dataModel.getDataByTag('plannedStop');
            // 八厂
            workshop1 = dataModel.getDataByTag(workshopCode1);
            workshop2 = dataModel.getDataByTag(workshopCode2);
            workshop3 = dataModel.getDataByTag(workshopCode3);
            // 九厂
            workshop_nine1 = dataModel.getDataByTag(workshopCode_nine1);
            workshop_nine21 = dataModel.getDataByTag(workshopCode_nine21);
            workshop_nine22 = dataModel.getDataByTag(workshopCode_nine22);
            workshop_nine3 = dataModel.getDataByTag(workshopCode_nine3);
            workshop_nine4 = dataModel.getDataByTag(workshopCode_nine4);
            iframe2 = dataModel.getDataByTag('iframe2');
            tableBtn = dataModel.getDataByTag('tableBtn');
            iframe = dataModel.getDataByTag('iframe');
        }


        // 返回按钮
        function workShopBackButton(backBthData) {
            if (backBthData.visible != null && backBthData.visible != undefined) {
                if ('true' === backBthData.visible) {
                    backButton.s('2d.visible', true);
                    backButton.a('url', Boolean(backBthData.data.url) ? backBthData.data.url : '');
                } else {
                    backButton.s('2d.visible', false);
                }
            }
        }

        // 报警灯
        function warningLights(lightData) {
            if (lightData.data != null && lightData.data != undefined && lightData.data != '') {
                normal.a('ht.value', Boolean(lightData.data.normal) ? lightData.data.normal : 0);
                fixedLength.a('ht.value', Boolean(lightData.data.fixedLength) ? lightData.data.fixedLength : 0);
                wireBreak.a('ht.value', Boolean(lightData.data.wireBreak) ? lightData.data.wireBreak : 0);
                changeover.a('ht.value', Boolean(lightData.data.changeover) ? lightData.data.changeover : 0);
                wireReplacement.a('ht.value', Boolean(lightData.data.wireReplacement) ? lightData.data.wireReplacement : 0);
                breakdown.a('ht.value', Boolean(lightData.data.breakdown) ? lightData.data.breakdown : 0);
                offLine.a('ht.value', Boolean(lightData.data.offLine) ? lightData.data.offLine : 0);
                plannedStop.a('ht.value', Boolean(lightData.data.plannedStop) ? lightData.data.plannedStop : 0);
            }
        }

        // 车间按钮显示  目前是把八厂和九厂的车间编码和数量固定了（八厂：3个，九厂：5个），根据接口展示
        function deptChange(deptData) {
            // 所有的车间编码
            let codeArr;
            // key:车间编码  value:车间dataModel
            let workshopMap = {};
            // 接口
            let dataMap = Boolean(deptData.data) ? deptData.data : null;
            // 接口中的所有key(车间编码)
            let keyArr = Object.keys(dataMap);
            // 5个车间位置
            let positionXArr = [136.80134, 244.65801, 352.65801, 460.65801, 568.65801];
            let positionY = 145.35826;

            if("XD1112" === strDeptCode){
                codeArr = ["HT_SC_ZZ_MAPTAB_XD111203", "HT_SC_ZZ_MAPTAB_XD111204", "HT_SC_ZZ_MAPTAB_XD111205"];
                workshopMap = {
                    "HT_SC_ZZ_MAPTAB_XD111203": workshop1,
                    "HT_SC_ZZ_MAPTAB_XD111204": workshop2,
                    "HT_SC_ZZ_MAPTAB_XD111205": workshop3
                };
                // 默认标签按钮隐藏
                workshop1.s('2d.visible', false);
                workshop2.s('2d.visible', false);
                workshop3.s('2d.visible', false);

            }else if("XD1113" === strDeptCode) {
                codeArr = ["HT_SC_ZZ_MAPTAB_XD111303", "HT_SC_ZZ_MAPTAB_XD11130401", "HT_SC_ZZ_MAPTAB_XD11130402", "HT_SC_ZZ_MAPTAB_XD111305", "HT_SC_ZZ_MAPTAB_XD111306"];
                workshopMap = {
                    "HT_SC_ZZ_MAPTAB_XD111303": workshop_nine1,
                    "HT_SC_ZZ_MAPTAB_XD11130401": workshop_nine21,
                    "HT_SC_ZZ_MAPTAB_XD11130402": workshop_nine22,
                    "HT_SC_ZZ_MAPTAB_XD111305": workshop_nine3,
                    "HT_SC_ZZ_MAPTAB_XD111306": workshop_nine4
                };
                // 默认标签按钮隐藏
                workshop_nine1.s('2d.visible', false);
                workshop_nine21.s('2d.visible', false);
                workshop_nine22.s('2d.visible', false);
                workshop_nine3.s('2d.visible', false);
                workshop_nine4.s('2d.visible', false);
            }

            // 遍历所有车间，与接口中匹配且有权限时，设置可见和位置

            // 位置标识，positionXArr下标
            let flag = 0;
            let workshopTmp = '';
            for (var i = 0; i < codeArr.length; i++) {
                workshopTmp = workshopMap[codeArr[i]];
                // 当前循环的车间编码
                var key = codeArr[i];
                // 判断接口中是否存在该key
                if (dataMap.hasOwnProperty(key)) {
                    var value = dataMap[key];
                    var visible = value['visible'];
                    if (Boolean(value) && visible === "true") {//有权展示
                        // 将该车间标签设为可见
                        workshopTmp.s('2d.visible', true);
                        //最近位置flag +1
                        flag++;
                        // 设置该车间标签位置
                        workshopTmp.setPosition(positionXArr[flag], positionY);
                    } else {//无权展示
                        workshopTmp.s('2d.visible', false);
                    }
                } else {//不在接口里
                    workshopTmp.s('2d.visible', false);
                }
            }
        }

        // 接收子页面workshop_table.html传值
        let param = '';
        window.addEventListener('message', function (e) {
            param = e.data;
            tableBtn.a('isSelected', false);
            iframe2.s('2d.visible', false);
            // 给子页面index.html传值
            messageParam.flag = "table";
            messageParam.message = param;
            iframeMessage(messageParam);
        }, false);

    </script>
</head>

<body onload='setTimeout(init, 300)'>
</body>

</html>